//
//  TimeUtils.swift
//  Reader17kSDK
//
//  Created by 朴社长 on 2020/11/24.
//

import Foundation

public extension Int {
    var date: Date {
        TimeInterval(self).date
    }
}

public extension TimeInterval {
    var date: Date {
        Date(timeIntervalSince1970: self)
    }
}

public extension Date {
    /// 返回当前 Date 到目标 Date 的毫秒差（有正负区分）
    func millisecondsSince(_ date: Date) -> Int {
        return Int((self.timeIntervalSince(date)) * 1000)
    }
    
    func secondsSince(_ date: Date) -> Int {
        return Int(self.timeIntervalSince(date))
    }
}

extension Date {
    
    /// 获取当前 秒级 时间戳 - 10位
    public var timeStampStr : String {
        return "\(timeStamp)"
    }
    
    public var timeStamp : Int {
        let timeInterval: TimeInterval = self.timeIntervalSince1970
        return Int(timeInterval)
    }
    
    public var standardTime : String {
        return timeStr()
    }
    
    public var milliStamp : CLongLong {
        let timeInterval: TimeInterval = self.timeIntervalSince1970
        return CLongLong(round(timeInterval*1000))
    }
    
    /// 获取当前 毫秒级 时间戳 - 13位
    public var milliStampStr : String {
        let timeInterval: TimeInterval = self.timeIntervalSince1970
        let millisecond = CLongLong(round(timeInterval*1000))
        return "\(millisecond)"
    }
    
    public func timeStr(dateFormat: String = "YYYY-MM-dd HH:mm:ss") -> String {
        
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = dateFormat
        let time = dateFormatter.string(from: self)
        return time
    }
    
    public func callTimeAfterNow() -> String {
        let timeInterval = Date().timeIntervalSince(self)
        if timeInterval < 0 {
            return "刚刚"
        }
        let interval = fabs(timeInterval)
        let i60 = interval/60
        let i3600 = interval/3600
        let i86400 = interval/86400
        let i2592000 = interval/2592000
        
        var time:String!
        
        if i3600 < 1 {
            let s = NSNumber(value: i60 as Double).intValue
            if s == 0 {
                time = "刚刚"
            } else {
                time = "\(s)分钟前"
            }
        } else if i86400 < 1 {
            let s = NSNumber(value: i3600 as Double).intValue
            time = "\(s)小时前"
        } else if i2592000 < 1 {
            let s = NSNumber(value: i86400 as Double).intValue
            time = "\(s)天前"
        }else {
            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = "yyyy-MM-dd"
            time = dateFormatter.string(from: self)
        }
        
        return time
    }
    
    public static func getDate(timeString: String, formatString: String? = "YYYY-MM-dd HH:mm:ss") -> Date? {
        let formatter = DateFormatter()
        formatter.dateStyle = .medium
        formatter.timeStyle = .short
        formatter.dateFormat = formatString ?? "YYYY-MM-dd HH:mm:ss"
        return formatter.date(from: timeString)
    }
    
    //MARK: - 时间转时间戳
    /// - Parameters:
    ///   - timeString: 时间字符串
    ///   - formatString: 格式（默认 YYYY-MM-dd HH:mm:ss）
    /// - Returns: 时间戳（秒级别）
    public static func getTimestmp(timeString: String, formatString: String? = "YYYY-MM-dd HH:mm:ss") -> Int64 {
        guard let date = getDate(timeString: timeString,formatString: formatString) else { return 0 }
        return Int64(date.timeIntervalSince1970)
    }
    
    public static func getDayString(timeString: String, formatString: String? = "YYYY-MM-dd HH:mm:ss") -> String {
        
        guard let date = getDate(timeString: timeString,formatString: formatString) else { return "" }
        
        let timeInterval = Date().timeIntervalSince(`date`)
        if timeInterval < 360*24*3600 {
            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = "MM月dd日"
            let time = dateFormatter.string(from: date)
            return time
        }
        
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "YYYY年MM月dd日"
        let time = dateFormatter.string(from: date)
        return time
    }
    
    public static func getTimeString(timeString: String, formatString: String? = "YYYY-MM-dd HH:mm:ss") -> String {
        
        guard let date = getDate(timeString: timeString,formatString: formatString) else { return "" }
        
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "HH:mm"
        let time = dateFormatter.string(from: date)
        return time
    }
}
